package com.example.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.domain.Station;
import com.example.service.StationService;

import net.sf.json.JSONArray;

@Controller
public class StationController {

	@Autowired
	StationService stationService;
	
	@RequestMapping("/api/stations/{lineId}")
	public String getStationsOfLine(@PathVariable(value="lineId") Integer lineId, Model model){
		List<Station> list = stationService.getStationsByLine(lineId); 
		model.addAttribute("stationList", list);
		return "metro/station";
	}
	
	@RequestMapping("/api/stages/{lineId}")
	@ResponseBody
	public JSONArray getStationsOfLine(@PathVariable(value="lineId") Integer lineId){
		List<Station> list = stationService.getStationsByLine(lineId); 
		return JSONArray.fromObject(list);
	}
	
	@RequestMapping("/api/station")
	public List<Station> getStationInfo(@RequestParam(value="stationName", required=true) String stationName){
		return stationService.getStationInfoByStationName(stationName);
	}
	
	@RequestMapping("/api/station/index")
	public String stationIndex(){
		return "metro/station";
	}
}
